package com.jeeplus.modules.home.web;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeeplus.common.persistence.BaseEntity;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.modules.file.entity.FileManage;
import com.jeeplus.modules.file.service.FileManageService;
import com.jeeplus.modules.oa.entity.OaNotify;
import com.jeeplus.modules.oa.service.OaNotifyService;
import com.jeeplus.modules.sys.echartDto.ResultProjectTypeDto;
import com.jeeplus.modules.sys.entity.Dict;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Result;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.DictService;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.ResultService;
import com.jeeplus.modules.sys.service.SystemService;

@Controller
@RequestMapping(value = "${adminPath}/home")
public class HomeController {

	@Autowired
	private FileManageService fileManageService;
	
	@Autowired
	private OaNotifyService oaNotifyService;
	
	@Autowired
	private OfficeService officeService;

	@Autowired
	private SystemService systemService;
	
	@Autowired
	private ResultService resultService;
	
	@Autowired
	private DictService dictService;
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index"})
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {

		// 资料共享
		FileManage fileManage = new FileManage();
		fileManage.setStatus(1);
		Page<FileManage> pageParm = new Page<FileManage>(request, response);
		pageParm.setPageSize(3);
		Page<FileManage> filePage = fileManageService.findPage(pageParm, fileManage); 
		model.addAttribute("filePage", filePage);
		
		// 通知公告
		OaNotify oaNotify = new OaNotify();
		//oaNotify.setSelf(true); 
		oaNotify.setType("2"); //系统通告
		Page<OaNotify> notifyparm = new Page<OaNotify>(request, response);
		notifyparm.setPageSize(3);
		Page<OaNotify> notifyPage = oaNotifyService.find(notifyparm, oaNotify); 
		model.addAttribute("notifyPage", notifyPage);
		
		// 优秀科研人员
		User user = new User();
		user.setExcellent(1);
		List<User> users = systemService.findUserByExcellent(user);
		model.addAttribute("users", users);
		
		// 优秀科研所
		Office office = new Office();
		office.setExcellent(1);
		List<Office> offices = officeService.findOfficeByExcellent(office);
		model.addAttribute("offices", offices);
		
		//机构列表
		List<Office> officeList = officeService.findNotRootOffice();
		if(officeList != null && officeList.size()>4){
			officeList = officeList.subList(0, 4);
		}
		model.addAttribute("deptList", officeList);
		
		return "modules/home/index";
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"resultIndex"})
	public String resultIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		// 机构列表
		List<Office> officeList = officeService.findNotRootOffice();
		model.addAttribute("officeList", officeList);
		
		// 成功发布量统计
		List<Office> offices = officeService.findNotRootOffice();
		List<ResultProjectTypeDto> resultRepub = resultService.selectResultRepub();
		if (offices != null && offices.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for (Office office : offices) {
				boolean flag = true;
				for (ResultProjectTypeDto resultProjectTypeDto : resultRepub) {
					if(StringUtils.equals(resultProjectTypeDto.getName(), office.getId())) {
						sb.append("{value:"+resultProjectTypeDto.getValue()+", name:'"+office.getName()+"'},");
						flag =false ;
						break;
					}
				}
				if(flag) {
					sb.append("{value:0, name:'"+office.getName()+"'},");
				}
			
			}
			sb.substring(0, sb.toString().length()-1);
			model.addAttribute("officeCount", sb.toString());
		}
		
		// 历年成果统计
		Dict dict = new Dict();
		dict.setType("courseType");
		List<Dict> dicts = dictService.findList(dict);
		if (dicts != null && dicts.size() > 0) {
			Calendar date = Calendar.getInstance();
			int nowYear =  date.get(Calendar.YEAR);
			int[] years = {nowYear, nowYear-1, nowYear-2};
			StringBuffer sb = new StringBuffer();
			StringBuffer type = new StringBuffer();
			for (Dict d : dicts) {
				sb.append("{name: '"+d.getLabel()+"',type: 'bar',data: [");
				for (int i = 0; i < years.length; i++) {
					Result result = new Result();
					result.setTopicType(d.getValue());
					result.setStartDate(years[i]+"-01-01 00:00:00");
					if ((years[i]%4==0)&&(years[i]%100!=0)||(years[i]%400==0)) {
						result.setEndDate(years[i]+"-12-31 23:59:59");
					} else {
						result.setEndDate(years[i]+"-12-30 23:59:59");	
					}
					Integer count = resultService.findCountByDate(result);
					if (count != null) {
						sb.append(count).append(",");
					} else {
						sb.append(0).append(",");
					}
				}
				sb.substring(0, sb.toString().length()-1);
				sb.append("]},");
				type.append("'").append(d.getLabel()).append("',");
			}
			sb.substring(0, sb.toString().length());
			model.addAttribute("resutlYear", "'"+nowYear+"','"+(nowYear-1)+"','"+(nowYear-2)+"'");
			model.addAttribute("resutlCount", sb.toString());
			model.addAttribute("resutlType", type.substring(0, type.toString().length()-1));
		}
		
		return "modules/home/resultIndex";
	}
	
}
